// ==UserScript==
// @name xg-Reader
// @namespace http://code.google.com/p/xg-reader/
// @description by SiuTin
// @version 1.2.0
// @build-time 2011-12-08 06:49:32
// @include http://www.google.com/reader/*
// @run-at document-end
// ==/UserScript==

/*
 http://www.gnu.org/licenses/gpl.html 
*/

/*
 * Copyright (C) 2011  siutin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


var SxCall = function (selector,condition,callback){
	 //console.log("selector: " + selector);
	 var obj = jQuery(selector);
	 //console.log(obj);
	 var args = Array.prototype.slice.call(obj,0);	
	 if( typeof obj === 'undefined' || condition.apply(this,args) ){
		console.log("SxCall('" + selector + "',(" + condition + "),(" + callback+ "))");
		window.setTimeout("SxCall('" + selector + "',(" + condition + "),(" + callback+ "))",50);	
	}else{
		if( typeof callback === 'function'){				
				callback.apply(this,args);
		}
	}	
}

var workflag={w1:false,w2:false,w3:false};

var defaultCondition = function(obj){ return obj.length == 0 }
				
var HideTopBar = function(obj){ 
			console.log("HideTopBar");
			jQuery(obj).hide();
			 workflag.w1 = true
}; 

var HideGb = function(obj){
			console.log("HideGb");
			jQuery(obj).hide(); 
			workflag.w2 = true
}; 

var AddSearchButton = function(obj){
	console.log("AddSearchButton");
	
	jQuery("<span/>",{
				role:"button",
				id:"googleReaderfix-switch-search",
				css:{"-webkit-user-select":"none",},
				class:"goog-inline-block jfk-button jfk-button-standard"
			})
			.appendTo(obj)			
			.bind("click",function(){
				var getData = jQuery(this).data("value");
				var value = getData != null ? getData : true;
				if(value == true)
					jQuery("#top-bar").slideDown();
				else
					jQuery("#top-bar").slideUp();
					
				jQuery(this).find("div:first-child").text((!value?"Show":"Hide") + " SearchBar");
				jQuery(this).data("value",!value);
			});
			
	jQuery("<div>",{
					html:"Show SearchBar",
					class:"goog-inline-block goog-flat-menu-button-caption"
				})
				.appendTo("#googleReaderfix-switch-search");
				
	workflag.w3 = true;	
}
var AddGbButton = function(obj){
	console.log("AddGbButton");
	
	jQuery("<span/>",{
				role:"button",
				id:"googleReaderfix-switch-gb",
				css:{"-webkit-user-select":"none"},
				class:"goog-inline-block jfk-button jfk-button-standard"
			})
			.appendTo(obj)
			.data("value",true)
			.bind("click",function(){
				var value = jQuery(this).data("value");
				if(value == true)
					jQuery("#gb").slideDown();
				else
					jQuery("#gb").slideUp();

				jQuery(this).data("value",!value);
				jQuery(this).find("div:first-child").html((!value ? "Show" : "Hide") + " Menu");
			});

	jQuery("<div>",{
					html:"Show Menu",
					class:"goog-inline-block goog-flat-menu-button-caption"
				})
				.appendTo("#googleReaderfix-switch-gb");
				
	workflag.w3 = true;	
}
var showBody = function(obj){
		console.log("showBody");
		
		jQuery(document.body).delay(1500).fadeIn("slow");
}

var CustomFunction = function(){
	SxCall("#gb", defaultCondition, HideGb);
	SxCall("#top-bar", defaultCondition, HideTopBar);
	SxCall("#viewer-top-controls", defaultCondition, AddSearchButton);
	SxCall("#viewer-top-controls",defaultCondition, AddGbButton);
	SxCall(document.body, function(obj){!(workflag.w1 == true && workflag.w2 == true && workflag.w3 == true)}, showBody);
	console.log("done.");
}

console.log("customscript was loaded.");
